package com.test.liuhb.test;

import com.android.print.sdk.Barcode;
import com.android.print.sdk.PrinterConstants;
import com.android.print.sdk.PrinterInstance;
import com.android.print.sdk.Table;

import com.test.liuhb.test.Util.Util;
import com.test.liuhb.test.hktable.DataRow;
import com.test.liuhb.test.hktable.DataTable;

import static com.test.liuhb.test.Util.Util.moneyFormat;


/**
 * Created by liu.hb on 2017/12/7.
 * 单据打印的工具类
 */
public class PrinterUitl {

    public static void printString(String str, PrinterInstance mPrinter){
        mPrinter.init();

        Table table=new Table("col", "223",new int[]{58,58});
        table.setColumnAlignRight(true);
        mPrinter.printTable(table );
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
       // mPrinter.printText("颜色:" + table.rows.get(0).get("ColorName"));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
    }
    public static void printText(DataTable table, PrinterInstance mPrinter) {

        mPrinter.init();
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, 1);
        mPrinter.printText("货号:" + table.rows.get(0).get("MaterialCode"));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("名称:" + table.rows.get(0).get("MaterialShortName"));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("尺码:" + table.rows.get(0).get("SizeName"));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("颜色:" + table.rows.get(0).get("ColorName"));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
    }

    public static void printSend(String[] arr, PrinterInstance mPrinter) {
        mPrinter.init();
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, 1);
        mPrinter.printText("单号:" + arr[0]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("收货人:" + arr[1]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("手机:" +arr[2]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("固话:" + arr[3]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("邮编:" + arr[4]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("地址:" + arr[5]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("物流方式:" + arr[6]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("物流公司:" + arr[7]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("是否开发票:" + arr[8]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("备注:" + arr[9]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
    }

    public static void printSmallTicket(DataTable table,String[] params, PrinterInstance mPrinter){
        mPrinter.init();
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
        mPrinter.printText(params[0]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("单号:" + params[1]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("收银员:" + params[2]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText(params[3]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("会员卡号:" + params[4]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("导购:" + params[5]);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);

        byte[] b ={95,95, 95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95
                ,95,95,95,95,95,95,95,95,95,95,95,95,95};
        mPrinter.printText(new String(b));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("品名     尺码     数量     金额");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText(new String(b));
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        DataRow dr;
        for(int i=0;i<table.getRowsCount();i++){
             dr=table.getRows().get(i);
            System.out.println(i+"table.getRows().get(i)");
//            mPrinter.setPrinter(PrinterConstants.Command.ALIGN_LEFT, 1);
//            mPrinter.setPrinter(PrinterConstants.Command.FONT_SIZE,10);
//            mPrinter.setPrinter(PrinterConstants.Command.FONT_MODE,18);

            mPrinter.printText(dr.get("MaterialShortName")+" ");
            mPrinter.printText(dr.get("SizeName")+" ");
            mPrinter.printText(dr.get("PickQty")+" ");
            mPrinter.printText(dr.get("balaPrice"));
            mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
            mPrinter.printText("售:"+dr.get("Price")+" ");
            double DiscountPrice= Util.toDouble(table.getRows().get(i).get("Price"))
                    -Util.toDouble(table.getRows().get(i).get("balaPrice"));
            table.getRows().get(i).set("Discount",moneyFormat(DiscountPrice)+"");
            System.out.println("-------原价:-----------"+dr.get("Price"));
            System.out.println("-------售:-----------"+dr.get("balaPrice"));
            System.out.println("-------折:-----------"+dr.get("Discount"));
            mPrinter.printText("折:"+dr.get("Discount")+" ");
            mPrinter.printText(params[6]);
            mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
            mPrinter.printText(new String(b));

        }
        mPrinter.printText("仟  佰  拾  元  角  分");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("共计"+table.getRowsCount()+"件商品");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("应收金额:"+params[7]);

        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText(new String(b));
        mPrinter.printText("储值卡:"+"           赠送:.00");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("刷卡:.00"+"         积分抵现:.00");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("优惠劵金额:");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("收款:"+params[7]+""+"  找零:.00");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText(new String(b));

        mPrinter.setPrinter(PrinterConstants.Command.ALIGN_LEFT, 1);
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, 1);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("谢谢惠顾!");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("请妥善保管此收银条.");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("退换货品以此收银条为凭.");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("祝您购物愉快!");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("Thank you for your shopping");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("at our store!");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("Please Keep this receipt");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("as proof of");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText("porchase return or exchange.");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
        mPrinter.printText(" Have a nice day!");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
    }



    public static void printBarCode(String barcode, PrinterInstance mPrinter) {
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, 1);
        Barcode barcode0 = new Barcode(PrinterConstants.BarcodeType.CODE128, 2, 150, 2, barcode);
        mPrinter.printBarCode(barcode0);
    }


}
